package com.cennavi.bigdata.baotou.scripts;

import com.cennavi.bigdata.baotou.service.ViewService;
import com.cennavi.bigdata.baotou.util.DateUtils;
import com.mongodb.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * Created by zhouxiaolong on 2017/4/12.
 */

public class ActualTimeKakouData extends ActualTimeRequest {

    private final ViewService viewService;
    private DBCollection kakouCol;
    private static final Logger LOGGER = LoggerFactory.getLogger(ActualTimeKakouData.class);

    public ActualTimeKakouData(ViewService viewService){
        this.viewService = viewService;
    }
    /**
     * 刷新行政区域指数数据
     */
    public void refrashKakouData() {
        kakouCol = getMongoCollection("kakou_data_history");
        String starTime = DateUtils.DateFormatUnit.DATE_AND_MIN.getDateStr(DateUtils.addHour(new Date(),-1));
        String endTime = DateUtils.DateFormatUnit.DATE_AND_MIN.getDateStr(new Date());
        String kakouDataG = viewService.getKakouCarStatis(starTime, endTime, "G");
        LOGGER.info("刷新卡口通车数据国道" );
        String kakouDataS = viewService.getKakouCarStatis(starTime, endTime, "S");
        LOGGER.info("刷新卡口通车数据省道" );
        String kakouDataX = viewService.getKakouCarStatis(starTime, endTime, "X");
        LOGGER.info("刷新卡口通车数据县道" );

        BasicDBObject data1 = BasicDBObject.parse(kakouDataG);
        data1.put("update_time", DateUtils.DateFormatUnit.DATE_AND_HOUR.getDateStr(DateUtils.addHour(new Date(), -1)));
        data1.put("dev_type", "G");
        kakouCol.insert(data1);

        BasicDBObject data2 = BasicDBObject.parse(kakouDataS);
        data2.put("update_time", DateUtils.DateFormatUnit.DATE_AND_HOUR.getDateStr(DateUtils.addHour(new Date(), -1)));
        data2.put("dev_type", "S");
        kakouCol.insert(data2);

        BasicDBObject data3 = BasicDBObject.parse(kakouDataX);
        data3.put("update_time", DateUtils.DateFormatUnit.DATE_AND_HOUR.getDateStr(DateUtils.addHour(new Date(), -1)));
        data3.put("dev_type", "X");
        kakouCol.insert(data3);

        closeMongo();
    }

}
